#!/bin/bash

echo -e "阿新的云存储服务器....\n"
echo -e "系统变量：$JPOM_VERSION,$JPOM_SERVER,$JPOM_AGENT,$JDK_VERSION...\n"
if [ "$JDK_VERSION" = "8" ]; then
    sed -i 's/java-17-openjdk/java-8-openjdk/g' /root/.bashrc
else
    sed -i 's/java-8-openjdk/java-17-openjdk/g' /root/.bashrc
fi
source /root/.bashrc
echo "root:$ROOT_PASSWD" | chpasswd
service ssh restart
service cron restart
service nginx restart
echo -e "\n"

if $JPOM_SERVER; then
    echo "启动Jpom服务端..."
    if [ -d "/root/jpom-server/bin" ]; then
        echo "Server目录存在"
    else
        echo "Server目录不存在"
        curl -o /root/server-release.tar.gz https://d.jpom.download/release/$JPOM_VERSION/server-$JPOM_VERSION-release.tar.gz
        cd /root && mkdir -p jpom-server
        tar -xzvf server-release.tar.gz -C ./jpom-server && rm -rf *-release.tar.gz
    fi
    cd /root/jpom-server || exit
    chmod 755 ./bin/*.sh
    ls -l ./bin
    nohup ./bin/Server.sh start 2>&1 &
    sleep 5
fi

if $JPOM_AGENT; then
    echo "启动Jpom插件端..."
    if [ -d "/root/jpom-agent/bin" ]; then
        echo "Agent目录存在"
    else
        echo "Agent目录不存在"
        curl -o /root/agent-release.tar.gz https://d.jpom.download/release/$JPOM_VERSION/agent-$JPOM_VERSION-release.tar.gz
        cd /root && mkdir -p jpom-agent
        tar -xzvf agent-release.tar.gz -C ./jpom-agent && rm -rf *-release.tar.gz
    fi
    cd /root/jpom-agent || exit
    chmod 755 ./bin/*.sh
    ls -l ./bin
    nohup ./bin/Agent.sh start 2>&1 &
    sleep 5
fi

while true; do
    sleep 5
done
